package com.huawei.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huawei.constant.MessageConstant;
import com.huawei.dao.CheckItemDao;
import com.huawei.entity.PageResult;
import com.huawei.entity.QueryPageBean;
import com.huawei.entity.Result;
import com.huawei.pojo.CheckItem;
import com.huawei.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //查询条件
        String queryString = queryPageBean.getQueryString();
        //每页查询的条数
        Integer pageSize = queryPageBean.getPageSize();
        //开始分页，ThreadLocal线程控制
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkItemDao.findPage(queryString);
        long total = page.getTotal();
        List<CheckItem> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void delete(Integer id) {
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count>0) {
            //检查项被检查组关联了,不能删除
              new RuntimeException();
        }
        checkItemDao.deleteByCheckItemId(id);
    }

    @Override
    public CheckItem findByCheckItemId(Integer id) {
        return checkItemDao.findByCheckItemId(id);
    }

    @Override
    public void editCheckItem(CheckItem checkItem) {
         checkItemDao.editCheckItem(checkItem);
    }


}
